package com.myblog.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myblog.Mapper.UserMapper;
import com.myblog.Mapper.typemapper;
import com.myblog.po.Blog;
import com.myblog.po.Type;
import com.myblog.service.BlogService;
import com.myblog.service.CommentService;
import com.myblog.vo.BlogQuery;
import com.myblog.vo.TypeQuery;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController<TypeService> {

    @Autowired
    private BlogService blogService;
    @Autowired
    private typemapper typeService;
    @Autowired
    private UserMapper userService;

   /* @Autowired
    private TagService tagService;*/

    @Autowired
    private CommentService commentService;
/*@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable*/
    @RequestMapping("/")
    public String index(@RequestParam(value = "page",required = false) Integer page,
                        Model model){
        if (page == null)
        {
            page = 1;
        }


        PageHelper.startPage(page,8);
        List<Blog> blogs = blogService.listblog();

        for (Blog blog: blogs) {
            blog.setUser(userService.GetUserById(blog.getUser_id()));
            blog.setType(typeService.getType(blog.getType_id()));
        }


        PageInfo pageInfo = new PageInfo(blogs);

        List<TypeQuery> tq= typeService.listTypeTop();

        List<Type> types = new ArrayList<>();

        for (TypeQuery t : tq){

            Type ts = typeService.getType(t.getId());
            BlogQuery b = new BlogQuery();
            b.setTypeid(ts.getId());
            ts.setBlogs(blogService.listBlogbyTypeid(b));
            types.add(ts);
        }



        model.addAttribute("types",types);
        model.addAttribute("page",pageInfo);
        model.addAttribute("recommendBlogs",blogService.listRecommendBlogTop());



            System.out.println("---index---");

        return "index";
    }

    @RequestMapping("/blog")
    public String blog(/*@PathVariable Integer id,@PathVariable String name*/){
//

       // System.out.println("---blog---");

        return "blog";
    }

@PostMapping("/search")
/*@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable*/
public String search(@RequestParam(value = "page",required = false) Integer page,
                             @RequestParam String query, Model model)
        {

            if (page == null)
            {
                page = 1;
            }
            PageHelper.startPage(page,8);
            List<Blog> blogs = blogService.seekBlog(query);

            for (Blog blog: blogs) {
                blog.setType(typeService.getType(blog.getType_id()));
            }

            PageInfo pageInfo = new PageInfo(blogs);


            model.addAttribute("page",pageInfo);
            model.addAttribute("query",query);

            return "search";
        }


    @GetMapping("/blogg/{id}")
    public String bloge(@PathVariable Long id,Model model){

            Blog blog = blogService.getBlog(id);

            blogService.AddViews(id);

            blog.setUser(userService.GetUserById(blog.getUser_id()));

            model.addAttribute("blog",blog);
        model.addAttribute("comments",commentService.listCommentByBlogId(id));

        return "blog";
    }

    @GetMapping("/footer/newblog")
    public String newblogs(Model model){
        model.addAttribute("newblogs",blogService.listRecommendBlogTop());
        return "_fragments :: newblogList";
    }

}
